﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    Layout = null;
}
<script src="~/lib/layui/layui.js"></script>
<script src="~/js/js.cookie.js"></script>
<script src="~/js/moment.js"></script>
<script src="~/lib/jquery/dist/jquery.js"></script>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    <link href="//unpkg.com/layui@2.9.20/dist/css/layui.css" rel="stylesheet">
</head>
<body>
    <h1 class="bold-large" layui-icon-question>用户分配角色</h1>
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名选择</label>
            <div class="layui-input-block">
                <select name="interest" lay-filter="aihao" id="userId">
                    <option value="0" selected>请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-form-item">
                <label class="layui-form-label">角色选择</label>
                <div class="layui-input-block">
                    <!-- 这里不再放置初始的复选框，因为它会被动态生成的复选框替换 -->
                    <div id="rolesId"></div> <!-- 新的容器用于放置动态生成的复选框 -->
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="demo1" onclick="AddUserRole(event)">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>

    </form>
</body>
</html>

<script>
    var form = layui.form;
    var $ = layui.jquery;

    $(function () {
        GetUser()  // 用户
        GetRoles() //角色
    })
    //用户角色添加
    function AddUserRole(event) {
        var roleArr = [];
        $("[name='roles']:checked").each(function () {
            roleArr.push($(this).val());
        });
        let data = {
            userId: $("#userId").val(),
            roles: roleArr
        }
        console.log(data)
        $.ajax({
            url: '@ViewBag.RBACWrite' + "/api/UserRolePermission/AddUserRole",
            type: "post",
            data: JSON.stringify(data),
            dataType: "json",
            contentType: "application/json",
            success: res => {
                console.log(res)
                if (res.code == 0) {
                    alert(res.msg)
                } else {
                    alert(res.msg)
                }
            }
        })
        event.preventDefault(); // 阻止表单默认提交
    }


    // 用户列表
    function GetUser() {
        $.ajax({
            url: '@ViewBag.RBACWrite' + "/api/Users/ShowUsers",
            data: JSON.stringify({}),
            type: "post",
            dataType: "json",
            contentType: "application/json",
            success: res => {
                $.each(res.data, function () {
                    $("#userId").append(`<option value="${this.userId}">${this.userName}</option>`);
                });
                form.render('select'); // 重新渲染下拉框
            },
        });
    }

    // 角色列表
    function GetRoles() {
        $.ajax({
            url: '@ViewBag.RBACWrite' + "/api/Roles/QueryRoles",
            data: JSON.stringify({}),
            type: "post",
            dataType: "json",
            contentType: "application/json",
            success: res => {
                $("#checkboxContainer").empty();
                $("#rolesId").empty(); // 清空之前的复选框

                $.each(res.data, function () {
                    const checkbox = $(`<input type="checkbox" name="roles" title="${this.rolesName}" value="${this.rolesId}" template-id="role_${this.rolesId}" lay-skin="primary">`);
                    $("#rolesId").append(checkbox);
                });
                form.render(); //重新渲染整个表单
            },
        });
    }
</script>